import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

// 导入顶部进度条插件 js/css
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 使用顶部进度条插件
NProgress.start();
// 顶部进度条插件  隐藏加载小图标
NProgress.configure({
	showSpinner: false
});

const routes = [
	// 路由导航视图
	{
		path: "/",
		component: () => import("@/views/tadview/index.vue"),
		// 子路由
		children: [
			// 首页
			{
				path: "/",
				name: "home",
				meta: {
					title: '华强建材',
				}, // 此处加自定义字段
				component: () => import("../views/home/index.vue"),
			},
			// 分类
			{
				path: "/assort",
				name: "assort",
				meta: {
					// 页面标题
					title: '商品分类',
					// 自定义属性，进入此路由需要登录
					//   requireAuth: true
				},
				component: () => import("../views/assort/index.vue")
			},
			// 购物车
			{
				path: "/cart",
				name: "cart",
				meta: {
					// 页面标题
					title: '购物车',
					//   requireAuth: true
					keepAlive: false,
				},
				component: () => import("../views/Cart/index.vue")
			},

			// 我的
			{
				path: "/user",
				name: "user",
				meta: {
					// 页面标题
					title: '个人中心',
					// 自定义属性，进入此路由需要登录
					//   requireAuth: true
				},
				component: () => import("../views/user/index.vue")
			}
		]
	},
	// 登录
	{
		path: "/logon",
		name: "logon",
		meta: {
			// 页面标题
			title: '登录',
			// 自定义属性，进入此路由需要登录
			//   requireAuth: true
		},
		component: () => import("../views/user/logon/logon.vue")
	},
	// 注册
	{
		path: "/login",
		name: "login",
		meta: {
			// 页面标题
			title: '注册',
			// 自定义属性，进入此路由需要登录
			//   requireAuth: true
		},
		component: () => import("../views/user/logon/login.vue")
	},
	// 购物车---结算
	{
		path: "/settle_accounts",
		name: "settle_accounts",
		meta: {
			// 页面标题
			title: '提交订单',
			requireButh: true
		},
		component: () => import("../views/Cart/settle_accounts.vue")
	},
	// 设置
	{
		path: "/users/setting",
		name: "setting",
		meta: {
			// 页面标题,
			title: '设置',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("../views/user/users/user_setting.vue")
	},
	// 设置---个人资料
	{
		path: "/users/user_info",
		name: "user_info",
		meta: {
			// 页面标题
			title: '个人资料',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("../views/user/users/personal_data/index.vue")
	},
	// 设置---个人资料---修改资料
	{
		path: "/users/user_info/revise",
		name: "revise",
		meta: {
			// 页面标题
			title: '修改资料',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("../views/user/users/personal_data/revise.vue")
	},
	// 设置---个人资料---修改密码
	{
		path: "/users/user_info/Change_Password",
		name: "Change_Password",
		meta: {
			// 页面标题
			title: '修改密码',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("../views/user/users/personal_data/Change_Password.vue")
	},
	// 设置---个人资料 ---- 地址管理
	{
		path: "/users/address",
		name: "address",
		meta: {
			// 页面标题
			title: '地址管理',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("../views/user/users/personal_data/address/index.vue")
	},
	// 设置---个人资料 ---- 地址管理
	{
		path: "/users/address/Add",
		name: "Add",
		meta: {
			// 页面标题
			title: '添加地址',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("../views/user/users/personal_data/address/Add.vue")
	},
	// 设置---个人资料 ---- 地址管理
	{
		path: "/users/address/Edit/:id",
		name: "Edit",
		meta: {
			// 页面标题
			title: '编辑地址',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("../views/user/users/personal_data/address/Edit.vue")
	},
	// 设置---关于我们
	{
		path: "/users/About_us",
		name: "About_us",
		meta: {
			// 页面标题
			title: '关于我们',
			// 自定义属性，进入此路由需要登录
			//   requireAuth: true
		},
		component: () => import("../views/user/users/About_us.vue")
	},
	// 设置---协议规则
	{
		path: "/users/Protocol_rules",
		name: "Protocol_rules",
		meta: {
			// 页面标题
			title: '协议规则',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("@/views/user/users/Protocol_rules/index.vue")
	},
	// 分类---商品列表
	{
		path: "/assort/listdetailed/:id",
		name: "listdetailed",
		meta: {
			// 页面标题
			title: '商品列表',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("@/views/assort/listdetailed.vue")
	},
	// 首页---商品详情
	{
		path: "/home/homedetailed/:id",
		name: "homedetailed",
		meta: {
			// 页面标题
			title: '商品详情',
			// 自定义属性，进入此路由需要登录
			requireAuth: true
		},
		component: () => import("@/views/home/homedetailed.vue")
	},
	// 首页---搜索
	{
		path: "/home/goods_search",
		name: "goods_search",
		meta: {
			// 搜索
			title: '搜索',
		},
		component: () => import("@/views/home/goods_search.vue")
	},
	// 首页---搜索---搜索结果
	{
		path: "/home/goods_search/search_result/:id",
		name: "search_result",
		meta: {
			// 搜索
			title: '搜索结果',
		},
		component: () => import("@/views/home/search_result.vue")
	},
	// 错误页面 404
	{
		path: "*",
		name: "wrong",
		component: () => import("../views/wrong.vue")
	},
];

const router = new VueRouter({
	routes
});

// 动态修改页面标题
router.beforeEach((to, from, next) => {
	to.meta.title && (document.title = to.meta.title);
	next();
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
	// console.log(to, from);
	if (to.meta.requireAuth) {
		if (localStorage.getItem("token")) {
			next(true);
		} else {
			alert("权限不足，请先登录！");
			next("/logon");
		}
	} else {
		next(true);
	}
});


// 顶部进度条 路由离开隐藏
NProgress.done();

export default router;
